package com.rin.controller;

import com.rin.entity.Event;
import com.rin.service.DeleteService;
import com.rin.service.InsertService;
import com.rin.service.SelectService;
import com.rin.service.UpdateService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/13
 */

@RestController
@RequestMapping("/event")
public class EventController {

    @Resource
    private InsertService insertService;

    @Resource
    private DeleteService deleteService;

    @Resource
    private UpdateService updateService;

    @Resource
    private SelectService selectService;

    @PostMapping("insertEvent")
    public void insertEvent(@RequestBody Event event) {
        try {
            insertService.insert(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("deleteEvent")
    public void deleteEvent(@RequestBody Event event) {
        String entityId = String.valueOf(event.getEventId());
        deleteService.delete(event, entityId);
    }

    @PostMapping("deleteEventBatch")
    public void deleteEventBatch(@RequestBody List<Event> eventList) {
        for (Event event : eventList) {
            String entityId = String.valueOf(event.getEventId());
            deleteService.delete(event, entityId);
        }
    }

    @PostMapping("updateEvent")
    public void updateEvent(@RequestBody Event event) {
        String entityId = String.valueOf(event.getEventId());
        updateService.update(event, entityId);
    }

    @PostMapping("selectEvent")
    public Object selectEvent(@RequestBody Event event) {
        try {
            return selectService.select(event, String.valueOf(event.getEventId()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("selectEventBatch")
    public List<Object> selectEventBatch(@RequestBody Event event) {
        try {
            return selectService.selectBatch(event);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @PostMapping("initRepository")
    public void initRepository(@RequestBody Event event) {
        try {
            selectService.initRepository(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
